home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 1.toast / Sample Code / Archive / Graphics / QuickDraw GX / GX->PostScript Sample / GXToPostScript / Imaging Engine / IntersectShapeAndBitmap.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-09-28  |  4.1 KB  |  131 lines  |  [TEXT/MPS ]

  1. /*
  2.      File:        IntersectShapeAndBitmap.c
  3.  
  4.      Contains:    QuickDraw GX to PostScript conversion code.
  5.                          File contains routine that does IntersectShape, 
  6.                          allowing for Bitmap shapes as input
  7.  
  8.      Version:    Technology:    Quickdraw GX 1.1.x
  9.       
  10.      Copyright:    © 1991-1997 by Apple Computer, Inc., all rights reserved.
  11. */
  12.  
  13. #include <Types.h>
  14. #include <GXGraphics.h>
  15. #include "OffscreenLibrary.h"
  16.  
  17. #if GENERATINGCFM
  18.     #define _IntersectShapeAndBitmap            IntersectShapeAndBitmap
  19. #endif
  20.  
  21. /*******************************************
  22.  
  23.     Routine: IntersectShapeAndBitmap
  24.     
  25.     Routine is like intersect shape but works
  26.     if one or both of the shapes is a bitmap.
  27.     
  28.     It differs from IntersectShape in that it
  29.     is potentially distructive to the operand.
  30.     
  31.     This could be fixed, but unnecessary for the
  32.     Imaging Engine's use.
  33.     
  34. ********************************************/
  35. OSErr IntersectShapeAndBitmap(gxShape target, gxShape operand);
  36. OSErr IntersectShapeAndBitmap(gxShape target, gxShape operand)
  37.     {
  38.         gxShapeType        targetType;
  39.         gxShapeType        operandType;
  40.         offscreen            theOffscreen;
  41.         gxTransform        oldTransform;
  42.         gxTransform        tempTransform;
  43.         gxShape                offscreenShape;
  44.         gxBitmap            offscreenBits;
  45.         gxShape                eraseShape;
  46.         gxColor                eraseColor;
  47.         gxRectangle        tempRect;
  48.         gxShape                targetBounds, operandBounds;
  49.         gxPoint                bitLocation;
  50.     
  51.         targetType = GXGetShapeType(target);
  52.         operandType = GXGetShapeType(operand);
  53.     
  54.         if ( (targetType != gxBitmapType) && (operandType != gxBitmapType) ) {
  55.         
  56.             GXIntersectShape(target, operand);
  57.             
  58.         } else {
  59.  
  60.             if (operandType == gxBitmapType) {                    // swap em, we always want target to be the bitmap.
  61.                 
  62.                 gxShape        swapShape = GXCopyToShape(nil, target);
  63.                 
  64.                 GXCopyToShape(target, operand);
  65.                 GXCopyToShape(operand, swapShape);
  66.                 GXDisposeShape(swapShape);
  67.                 
  68.             }//end if
  69.             
  70.             
  71.             /** Find the intersection of their bounding boxes, to minimize bitmap size **/
  72.             
  73.             targetBounds = GXNewRectangle(GXGetShapeBounds(target, 0, &tempRect));
  74.             operandBounds = GXNewRectangle(GXGetShapeBounds(operand, 0, &tempRect));
  75.             GXIntersectShape(targetBounds, operandBounds);
  76.             GXGetShapeBounds(targetBounds, 0, &tempRect);
  77.             GXDisposeShape(targetBounds);
  78.             GXDisposeShape(operandBounds);
  79.  
  80.             /** Create an offscreen with the inersection rectangle **/            
  81.             
  82.             GXGetBitmap(target, &offscreenBits, nil);            // Use the target shape's bitmap as a template.
  83.             offscreenBits.image = nil;                                        // Make gx graphics allocate a new bit image.
  84.             
  85.             offscreenBits.width = tempRect.right - tempRect.left;
  86.             offscreenBits.height = tempRect.bottom - tempRect.top;
  87.             bitLocation.x = tempRect.left;
  88.             bitLocation.y = tempRect.top;
  89.             offscreenShape = GXNewBitmap(&offscreenBits, &bitLocation);
  90.             CreateOffscreen(&theOffscreen, offscreenShape);
  91.             
  92.             
  93.             /** Make sure the offscreen is clear **/
  94.             eraseShape = GXNewShape(gxFullType);
  95.             GXSetShapeViewPorts(eraseShape, 1, &(theOffscreen.port));
  96.             eraseColor.space = gxGraySpace;
  97.             eraseColor.profile = offscreenBits.profile;            // make sure it is same profile
  98.             eraseColor.element.gray = 0xFFFF;
  99.             GXSetShapeColor(eraseShape, &eraseColor);
  100.             GXDrawShape(eraseShape);
  101.             GXDisposeShape(eraseShape);
  102.             
  103.  
  104.             /** Intersect the two by drawing the oprand, clippped against target into a new offscreen **/
  105.  
  106.             oldTransform = GXCloneTransform(GXGetShapeTransform(operand));    // Save the operand's transform.
  107.             tempTransform = GXNewTransform();
  108.             GXSetTransformViewPorts(tempTransform, 1, &(theOffscreen.port));        // make it draw into offscreen.
  109.             GXSetTransformClip(tempTransform, target);                                                    // make the clip the target bitmap.
  110.             GXSetShapeTransform(operand, tempTransform);
  111.             GXDisposeTransform(tempTransform);                        // shape owns it now.    
  112.             
  113.             GXDrawShape(operand);                                                    // The offscreen now contains intersection shape which is a bitmap.
  114.  
  115.             GXSetShapeTransform(operand, oldTransform);        // restore the original transoform.
  116.             GXDisposeTransform(oldTransform);
  117.             
  118.             GXCopyToShape(target, offscreenShape);                // Make the new shape the target.
  119.             
  120.             /** Clean up **/
  121.             
  122.             DisposeOffscreen(&theOffscreen);
  123.             GXDisposeShape(offscreenShape);
  124.             
  125.             
  126.         }//end if
  127.  
  128.         return(GXGetGraphicsError(nil));
  129.     
  130.     }//IntersectShapeAndBitmap
  131.